package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.mapper.ContradictionRecordMapper;
import com.company.project.pojo.contradiction.Contradiction;
import com.company.project.mapper.ContradictionMapper;
import com.company.project.pojo.contradiction.ContradictionDTO;
import com.company.project.pojo.contradictionRecord.ContradictionRecord;
import com.company.project.pojo.roleVillage.RoleVillageDTO;
import com.company.project.service.IContradictionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 矛盾 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2019-08-09
 */
@Service
public class ContradictionServiceImpl extends ServiceImpl<ContradictionMapper, Contradiction> implements IContradictionService {

    @Autowired
    private ContradictionMapper contradictionMapper;
    @Autowired
    ContradictionRecordMapper contradictionRecordMapper;

    /*
    获取矛盾资料列表
     */
    @Override
    public IPage<ContradictionDTO> getContradictionListPage(Page page, String keyword, Integer status, Integer isPush, String startTime, String endTime) {
        IPage<ContradictionDTO> pageInfo = contradictionMapper.getContradictionList(page, keyword, status, isPush, startTime, endTime);

        List<ContradictionDTO> contradictionDTOList = new ArrayList<>();
        for (ContradictionDTO contradictionDTO: pageInfo.getRecords()) {
            QueryWrapper<ContradictionRecord> contradictionRecordQueryWrapper = new QueryWrapper<>();
            contradictionRecordQueryWrapper.eq("is_delete", 0);
            contradictionRecordQueryWrapper.eq("contradiction_id", contradictionDTO.getId());
            contradictionRecordQueryWrapper.orderByDesc("handle_time");
            List<ContradictionRecord> contradictionRecordList = contradictionRecordMapper.selectList(contradictionRecordQueryWrapper);

            if(contradictionRecordList.size() > 0){
                ContradictionRecord contradictionRecordLast = contradictionRecordList.get(0);
                contradictionDTO.setHandleStatus(contradictionRecordLast.getStatus());
            }

            contradictionDTOList.add(contradictionDTO);
        }
        pageInfo.setRecords(contradictionDTOList);

        return pageInfo;
    }

    /**
     * 查询推送给单位的矛盾资料
     */
    @Override
    public IPage<ContradictionDTO> getContradictionListByHandleInstitutionId(Page page, List<RoleVillageDTO> roleVillageDTOList, Integer institutionId, Integer roleId, Integer status) {
        IPage<ContradictionDTO> pageInfo = contradictionMapper.getContradictionListByHandleInstitutionId(page, roleVillageDTOList, institutionId, roleId, status);

        return pageInfo;
    }

    /**
     * 根据角色id查询矛盾资料
     */
    @Override
    public IPage<ContradictionDTO> getContradictionListByRoleId(Page page, List<RoleVillageDTO> roleVillageDTOList, Integer institutionId, Integer roleId, Integer status, String startTime, String endTime) {
        IPage<ContradictionDTO> pageInfo = contradictionMapper.getContradictionListByRoleId(page, roleVillageDTOList, institutionId, roleId, status, startTime, endTime);

        return pageInfo;
    }

    /**
    *获取管理区统计
     */
    @Override
    public List<ContradictionDTO> getManagementzoneStatistics(String startTime, String endTime) {
        List<ContradictionDTO> contradictionDTOList = contradictionMapper.getManagementzoneStatistics(startTime, endTime);
        return contradictionDTOList;
    }

    /**
     * 获取职能部门统计
     */
    @Override
    public List<ContradictionDTO> getDepartmentsStatistics(Integer institutionId, String startTime, String endTime) {
        List<ContradictionDTO> contradictionDTOList = contradictionMapper.getDepartmentsStatistics(institutionId, startTime, endTime);
        return contradictionDTOList;
    }

    /**
     *根据管理区获取村庄统计
     */
    @Override
    public List<ContradictionDTO> getVillageStatistics(Integer institutionId, Integer villageId, String startTime, String endTime) {
        List<ContradictionDTO> contradictionDTOList = contradictionMapper.getVillageStatistics(institutionId, villageId, startTime, endTime);
        return contradictionDTOList;
    }

    /**
    *查询待提交的矛盾资料
     */
    @Override
    public IPage<ContradictionDTO> getToBeSubmittedContradictionList(Page page, Integer user_id) {
        IPage<ContradictionDTO> pageInfo = contradictionMapper.getToBeSubmittedContradictionList(page, user_id);
        return pageInfo;
    }

    /**
     *获取登录人提交的矛盾资料列表
     */
    @Override
    public IPage<ContradictionDTO> getContradictionListPageByInputUserId(Page page, Integer user_id, Integer status, String startTime, String endTime) {
        IPage<ContradictionDTO> pageInfo = contradictionMapper.getContradictionListPageByInputUserId(page, user_id, status, startTime, endTime);
        return pageInfo;
    }
}
